package com.example.newbiechen.ireader.service.boluoxs;

import com.example.newbiechen.ireader.model.bean.BookChapterBean;
import com.example.newbiechen.ireader.model.bean.CollBookBean;
import com.example.newbiechen.ireader.utils.MD5Utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by robrk on 2019/3/10.
 */

public final class HtmlHelper {
    public static List<String> getTagText(String html, String tagStart, String tagEnd){
        Pattern p = Pattern.compile(tagStart +  "([\\s\\S]*?)" + tagEnd + "");
        Matcher m = p.matcher(html);
        List<String> lst = new ArrayList<>();
        while(m.find()){
//            System.out.println(m.group());
//            System.out.println("============================================");
//            int count = m.groupCount();
//            for(int i=0; i <  count; i++){
//                System.out.println(m.group(i));
//            }

            lst.add(m.group(1));
//            System.out.println(m.group(1));
        }
        return lst;
    }

    public static String getArticleText(String html){
        // <article id="nr"></article>
//                Pattern pattern = Pattern.compile("<article id=\"nr\"></article>");

        List<String> lst = HtmlHelper.getTagText(html, "<article id=\"nr\">", "</article>");
        if(lst.size() > 0){
            String str = lst.get(0).replace(" ", "");
            str = str.replace("&nbsp;", " ").replace("<br/>", "\n");
//            System.out.println(str);
            return str;
        }
        return "";
    }

    public static List<String> getMenuGroup(String html){
        List<String> lst = HtmlHelper.getTagText(html, "<div class=\"showpage r3\">", "</ul></div>");
        List<String> restutLst = new ArrayList<>();
        //<a href="/0/188_1/" class="xbk">1 - 40章</a>
        Pattern aPattern = Pattern.compile("<a[\\s\\S]+?href=\"([^\"]*?)\"[\\s\\S]+?class=\"xbk\"[\\s\\S]*?>([^<]*?)</a>");
        if(lst.size() > 0){
            Matcher m = aPattern.matcher(lst.get(0));
            while (m.find()){
//                System.out.println(m.group());
//                System.out.println("-1" + m.group(1));
//                System.out.println("-2" + m.group(2));
                restutLst.add(m.group(1));
            }
        }
        return restutLst;
    }

    public static List<BookChapterBean> getMenus(String html) throws Exception {
        // <ul class="lb fk"></ul>
//                Pattern ulPattern = Pattern.compile("");
        List<String> lst = HtmlHelper.getTagText(html, "<ul class=\"lb fk\">", "</ul>");
        List<BookChapterBean> beans = new ArrayList<>();
        // <li><a href="/0/188/15901834.html" class="xbk">第2713章 庙里女妖精</a></li>
//        Pattern aPattern = Pattern.compile("<a href=\"([^\"]*?)\" class=\"xbk\">([^<]*?)</a>");
        Pattern aPattern = Pattern.compile("<a[\\s\\S]+?href=\"([^\"]*?)\"[\\s\\S]+?class=\"xbk\"[\\s\\S]*?>([^<]*?)</a>");
        for (String str : lst) {
            Matcher m = aPattern.matcher(str);
            while (m.find()){
//                System.out.println(m.group());
//                System.out.println("-1" + m.group(1));
//                System.out.println("-2" + m.group(2));
                BookChapterBean chapterBean = new BookChapterBean();
                chapterBean.setTitle(m.group(2));
                chapterBean.setLink(m.group(1));
                chapterBean.setId(MD5Utils.strToMd5By16(chapterBean.getLink()));
                chapterBean.setStart(0L);
                chapterBean.setEnd(0L);
                chapterBean.setTaskName("");
                beans.add(chapterBean);
            }
        }
        if(beans.size() == 0){
            throw new Exception("获取目录失败");
        }
        return beans;
    }

    public static CollBookBean getBookDetail(String html){
        CollBookBean collBookBean = new CollBookBean();
        // <meta property="og:title" content="斗罗大陆IV终极斗罗"/>
        Pattern aPattern = Pattern.compile("<meta[\\s]+?property=\"([^\"]*?)\"[\\s\\S]+?content=\"([^\"]*?)\"[\\s]*?/>");
        Matcher m = aPattern.matcher(html);
        while (m.find()) {
//            System.out.println(m.group());
//            System.out.println(m.group(1));
//            System.out.println(m.group(2));

            String key = m.group(1);
            String value = m.group(2);
            switch (key){
                case "og:url":
                    collBookBean.set_id(MD5Utils.strToMd5By16(value));
                    collBookBean.setDetailUrl(value);
                    break;
                case "og:novel:author":
                    collBookBean.setAuthor(value);
                    break;
                case "og:description":
                    collBookBean.setShortIntro(value);
                    break;
                case "og:image":
                    try {
                        collBookBean.setCover("/agent/" + URLEncoder.encode(value, "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    break;
                case "og:novel:book_name":
                    collBookBean.setTitle(value+"(boluoxs)");
                    break;
                case "og:novel:update_time":
                    collBookBean.setUpdated(value);
                    break;
                case "og:novel:latest_chapter_name":
                    collBookBean.setLastChapter(value);
                    break;
            }
        }
        return collBookBean;
    }
}
